package com.zhumiao.weixindev.controller;

import com.zhumiao.weixindev.entity.WeixinMessage;
import com.zhumiao.weixindev.service.CharacterGameService;
import com.zhumiao.weixindev.util.CheckUtil;
import com.zhumiao.weixindev.util.XmlUtil;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

@RestController
public class ValidateController {
    @Autowired
    private CharacterGameService characterGameService;
    @GetMapping("/weixin")
    public void testWeixin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");

        PrintWriter writer = response.getWriter();

        if(CheckUtil.checkSignature(signature,timestamp, nonce)){
            writer.print(echostr);
        }
    }
    @PostMapping("/weixin")
    public void receiveAndSend(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter writer = response.getWriter();
        try {
            Map<String, String> map = XmlUtil.xmlToMap(request);
            String fromUserName = map.get("FromUserName");
            String toUserName = map.get("ToUserName");
            String msgType = map.get("MsgType");
            String content = map.get("Content");
            String mediaId = map.get("MediaId");
            String message = null;
            WeixinMessage textMessage = new WeixinMessage();
            textMessage.setFromUserName(toUserName);
            textMessage.setToUserName(fromUserName);
            textMessage.setCreateTime(new Date().getTime());
            textMessage.setMediaId(mediaId);
            if("text".equals(msgType)){
                textMessage.setContent("这是一个游戏(会随机生成一段话):" + characterGameService.randomMessage() + content);
                textMessage.setMsgType(msgType);
            } else if ("image".equals(msgType)){
                textMessage.setContent("请不要发送图片，还没有开发处理功能");
                textMessage.setMsgType("text");
            } else if("voice".equals(msgType)){
                textMessage.setContent("请不要发送语音，还没有开发处理功能");
                textMessage.setMsgType("text");
            }

            message = XmlUtil.textMessageToXml(textMessage);
            System.out.println(textMessage);
            writer.print(message);
        }  catch (DocumentException e) {
            e.printStackTrace();
        }   finally {
            writer.close();
        }
    }
}
